package com.cloudlead.common.lang.serialize;

import java.io.IOException;
import java.util.Date;

import com.cloudlead.common.lang.utils.DateTimeConvertUtils;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.DateDeserializers.DateDeserializer;

public class SimpleDateDeserializer extends DateDeserializer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 162800339949328475L;

	@Override
	public Date deserialize(JsonParser jp, DeserializationContext ctxt)
			throws IOException, JsonProcessingException {
		JsonToken t = jp.getCurrentToken();
		if (t == JsonToken.VALUE_NUMBER_INT) {
			return new java.util.Date(jp.getLongValue());
		}
		if (t == JsonToken.VALUE_NULL) {
			return (java.util.Date) getNullValue();
		}
		if (t == JsonToken.VALUE_STRING) {
			try {
				/*
				 * As per [JACKSON-203], take empty Strings to mean null
				 */
				String str = jp.getText().trim();
				if (str.length() == 0) {
					return (Date) getEmptyValue();
				}
				return DateTimeConvertUtils.convertToDate(str);
			} catch (IllegalArgumentException iae) {
				throw ctxt.instantiationException(
						_valueClass,
						"not a valid representation (error: "
								+ iae.getMessage() + ")");
			}
		}
		throw ctxt.mappingException(_valueClass, t);
	}

}
